package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("validatecode")
public class ValiDateCodeController {
    @RequestMapping("getCode")
    public void getCode(HttpSession session, HttpServletResponse response){
        //1、获得一个验证码对象  利用hutool工具类
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(100, 30);
        String code = captcha.getCode();
        System.out.println("验证码内容："+code);
//        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute("validatecode",code);
        //将验证码对象响应给浏览器
        try {
            captcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
